﻿using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VSSDK.Tools.VsIdeTesting;
using Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.Commands;
using System;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Linq;
using Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.Views;

namespace Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.Tests.Integration.Instruments
{
    public abstract class FxCopIntegratorTestAutomaterBase : VSPackageTestAutomaterBase<FxCopIntegratorPackage>
    {
        private static readonly string SolutionRoot = Path.Combine("..", "..", "..", "..", "..", "test", "integration");


        protected FxCopIntegratorPackage Package { get; private set; }


        protected FxCopIntegratorTestAutomaterBase()
        {
            Package = PackageLoader.LoadPackage<FxCopIntegratorPackage>();
        }


        protected override void OpenSolution(string relativePathFromSolutionRoot)
        {
            base.OpenSolution(Path.Combine(SolutionRoot, relativePathFromSolutionRoot));
        }

        protected virtual void ExecuteCommand<TCommand>()
            where TCommand : ICommand
        {
            var attr = typeof(TCommand).GetCustomAttributes(typeof(MenuCommandAttribute), false).Cast<MenuCommandAttribute>().FirstOrDefault();
            if (attr == null)
            {
                throw new InvalidOperationException();
            }
            ExecuteCommand(new Guid(attr.MenuGroupGuid), attr.CommandId);
        }

        protected virtual void ExecuteCommand<TCommand>(params UIControlAction[] actions)
            where TCommand : ICommand
        {
            var attr = typeof(TCommand).GetCustomAttributes(typeof(MenuCommandAttribute), false).Cast<MenuCommandAttribute>().FirstOrDefault();
            if (attr == null)
            {
                throw new InvalidOperationException();
            }
            ExecuteCommand(new Guid(attr.MenuGroupGuid), attr.CommandId, actions);
        }

        protected virtual void CloseAllWindows()
        {
            GetWindow<CodeInspectionResultWindowPane>().Close();
            GetWindow<CodeMetricsCalculationResultWindowPane>().Close();
        }
    }
}
